'''
Created on May 31, 2010

@author: Guan Gui, Will Zhang
$LastChangedBy: wilzzha $
'''

__version__ = "$Rev: 356 $"
import math

class ReorderingModel(object):
    """
    This class implements a Distance-Based Reordering Model for Phrase Based
    Translation Systems. 
    """
    def __init__(self, alpha = 0.7):
        """
        Create a new model
        @param alpha: The alpha value of the distance based reordering model
                        default = 0.70
        @type alpha: C{float}
        """
        if alpha > 1 or alpha < 0:
            raise Exception("Alpha out of range [0,1]")
        self.alpha = alpha

    def score(self, end_last_i, start_i):
        """
        Calculate reordering penalty based on a distance-based reordering model
        @param end_last_i: Ending position of the last phrase translated
        @type end_last_i: C{int}
        @param start_i: Starting position of the current phrase been translated
        @type start_i C{int}
        @return: The reordering penalty
        @rtype: C{float}
        """
        return math.pow(self.alpha, math.fabs(start_i - end_last_i - 1))
